const fs = require('fs')
const path = require('path')
const regStyle = /<style>[\s\S]*<\/style>/
const regScript = /<script>[\s\S]*<\/script>/
fs.readFile(path.join(__dirname, '/file/clock/clock.html'), 'utf-8', (_, str) => {
  resolveCss(str)
  resolveScript(str)
  resolveHTML(str)
})

const resolveCss = (htmlStr) => {
  const r1 = regStyle.exec(htmlStr)
  const newCss = r1[0].replace('<style>', '').replace('</style>', '')
  fs.writeFile(path.join(__dirname, '/file/clock/index.css'), newCss, 'utf-8', (_) => {
    console.log('css写入成功');
  })
}

const resolveScript = (htmlStr) => {
  const r1 = regScript.exec(htmlStr)
  const newScript = r1[0].replace('<script>', '').replace('</script>', '')
  fs.writeFile(path.join(__dirname, '/file/clock/index.js'), newScript, 'utf-8', (_) => {
    console.log('javascript写入成功');
  })
}

const resolveHTML = (htmlStr) => {
  const newHtml = htmlStr.replace(regStyle, '<link rel="stylesheet" href="./index.css">').replace(regScript, '<script src="./index.js"></script>')
  fs.writeFile(path.join(__dirname, '/file/clock/clock.html'), newHtml, 'utf-8', (_) => {
    console.log('写入HTML成功')
  })
}